﻿Imports System.Drawing
Imports System.Windows.Forms
Public Class Kennel

    Private watchDogs As New ArrayList
    Private isExit As Boolean

    Private Sub Kennel_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '
        Me.lvKennel.Items.Clear()

        Dim kennels() As String

        kennels = MainModule.Read("KENNEL_SECTION", "KENNEL_KEY").Split(",")

        If Not "".Equals(kennels(0)) Then
            For Each item As String In kennels

                Dim watchDog As New WatchDog(item)
                watchDogs.Add(watchDog)

                Dim lvi As New System.Windows.Forms.ListViewItem

                lvi = New System.Windows.Forms.ListViewItem(New String() {watchDog.dogName}, Me.lvKennel.Items.Count)
                Me.lvKennel.Items.Add(lvi)
                lvi = Nothing
                watchDog = Nothing
            Next
        End If

        Me.NotifyIcon1.Visible = False
        Me.NotifyIcon1.Icon = ToIcon(Me.ImageList2.Images.Item(0))
        isExit = False
    End Sub
    Private Function ToIcon(image As Image) As Icon
        ' 创建一个Bitmap对象，与Image具有相同的尺寸  
        Using bitmap As New Bitmap(image.Width, image.Height)
            ' 将Image绘制到Bitmap上  
            Using graphics As Graphics = Graphics.FromImage(bitmap)
                graphics.DrawImage(image, New Rectangle(0, 0, image.Width, image.Height))
            End Using
            ' 从Bitmap创建Icon  
            Return Icon.FromHandle(bitmap.GetHicon())
        End Using
    End Function


    Private Sub tmrWatcher_Tick(sender As Object, e As EventArgs) Handles tmrWatcher.Tick


        If Not MessageController.messagelist.Count = 0 Then
            Dim msg(1) As String
            msg = MessageController.messagelist.Item(0)
            MessageController.messagelist.RemoveAt(0)

            Dim thd As Threading.Thread
            thd = New Threading.Thread(AddressOf POPWindows)
            thd.Start(msg)
            Me.lbLog.Items.Add(Format(Now, "yyyy/MM/dd HH:mm:ss ") & "file: [ " & msg(0) & " ] =>" & msg(1))
        End If

    End Sub

    Private Sub POPWindows(ByVal obj As Object)

        Me.Invoke(New Action(Sub()
                                 Dim myDialog As New MessageWindow()
                                 myDialog.lblMonitoringFile.Text = obj(0)
                                 myDialog.lblAction.Text = obj(1)

                                 Select Case obj(2)
                                     Case WatchType.Create
                                         myDialog.Text = "ファイル新規"
                                     Case WatchType.Rename
                                         myDialog.Text = "ファイルリネーム"
                                     Case WatchType.Delete
                                         myDialog.Text = "ファイル削除"
                                     Case WatchType.Change
                                         myDialog.Text = "ファイル更新"
                                 End Select

                                 myDialog.TopMost = True
                                 myDialog.ShowDialog(Me)
                             End Sub))
    End Sub

    Private Sub lvKennel_MouseDoubleClick(sender As Object, e As EventArgs) Handles lvKennel.MouseDoubleClick
        Dim wl As New WatchList
        wl.dog = Me.watchDogs(lvKennel.SelectedItems.Item(0).Index)
        wl.iType = WatchEnemy.EnemyType.Upd
        wl.ShowDialog(Me)
    End Sub

    Private Sub tmrBrink_Tick(sender As Object, e As EventArgs) Handles tmrBrink.Tick

        For i As Integer = 0 To Me.lvKennel.Items.Count - 1
            Dim lvi As New ListViewItem
            lvi = Me.lvKennel.Items(i)

            Dim dog As WatchDog
            dog = Me.watchDogs(i)
            If dog.GetStatus Then
                'lvi.ImageIndex = (lvi.ImageIndex + 1) Mod 2
                Dim lastSecond As String
                lastSecond = Format(Now, "yyyyMMddHHmmss")
                lastSecond = lastSecond.Substring(lastSecond.Length - 1)

                Dim idx As Integer
                idx = lastSecond Mod 2
                If idx = 0 Then
                    lvi.ImageIndex = i
                Else
                    lvi.ImageIndex = 10
                End If

            End If
        Next

    End Sub

    Private Sub btnAllStart_Click(sender As Object, e As EventArgs) Handles btnAllStart.Click

        For i As Integer = 0 To Me.lvKennel.Items.Count - 1
            Dim lvi As New ListViewItem
            lvi = Me.lvKennel.Items(i)

            Dim dog As WatchDog
            dog = Me.watchDogs(i)
            dog.Start_Watch()
        Next

    End Sub

    Private Sub btnAllStop_Click(sender As Object, e As EventArgs) Handles btnAllStop.Click
        For i As Integer = 0 To Me.lvKennel.Items.Count - 1
            Dim lvi As New ListViewItem
            lvi = Me.lvKennel.Items(i)

            Dim dog As WatchDog
            dog = Me.watchDogs(i)
            dog.Stop_Watch()
        Next
    End Sub


    Private Sub STARTToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles STARTToolStripMenuItem.Click

        If lvKennel.SelectedItems.Count > 0 Then
            Dim dog As WatchDog
            dog = Me.watchDogs(lvKennel.SelectedItems.Item(0).Index)
            dog.Start_Watch()

            Me.ShowMessage("[ " & dog.dogName & " ] has been started!")
        End If

    End Sub

    Private Sub ShowMessage(ByVal strTmp As String)
        Me.lblMessage.Text = strTmp
    End Sub

    Private Sub STOPToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles STOPToolStripMenuItem.Click
        If lvKennel.SelectedItems.Count > 0 Then
            Dim dog As WatchDog
            dog = Me.watchDogs(lvKennel.SelectedItems.Item(0).Index)
            dog.Stop_Watch()

            Me.ShowMessage("[ " & dog.dogName & " ] has been stoped!")
        End If
    End Sub

    Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
        Me.Show()
        Me.NotifyIcon1.Visible = False
    End Sub

    Private Sub Kennel_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Not Me.isExit Then
            e.Cancel = True
            Me.Hide()
            Me.NotifyIcon1.Visible = True
        End If

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.isExit = True
        Me.Close()
    End Sub

    Private Sub tmrNotifyIconBrink_Tick(sender As Object, e As EventArgs) Handles tmrNotifyIconBrink.Tick
        If Me.NotifyIcon1.Visible = True Then
            Dim hasDogLive As Boolean = False
            For i As Integer = 0 To Me.lvKennel.Items.Count - 1
                Dim dog As WatchDog
                dog = Me.watchDogs(i)
                If dog.isPatroling Then
                    hasDogLive = True
                    Exit For
                End If
            Next

            If hasDogLive Then
                Dim lastSecond As String
                lastSecond = Format(Now, "yyyyMMddHHmmss")

                lastSecond = lastSecond.Substring(lastSecond.Length - 1)

                Me.NotifyIcon1.Icon = ToIcon(Me.ImageList2.Images.Item(CInt(lastSecond) Mod 2))


            End If

        End If

    End Sub

    Private Sub STARTToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles STARTToolStripMenuItem1.Click
        btnAllStart_Click(Nothing, Nothing)
    End Sub

    Private Sub STOPToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles STOPToolStripMenuItem1.Click
        btnAllStop_Click(Nothing, Nothing)
    End Sub

    Private Sub NEWToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NEWToolStripMenuItem.Click
        Dim wl As New WatchList
        wl.dog = New WatchDog("監視くん")
        wl.iType = WatchEnemy.EnemyType.Add
        wl.ShowDialog(Me)

        If wl.isSaved Then

            Me.watchDogs.Add(New WatchDog(wl.dog.dogName))
            Dim lvi As New System.Windows.Forms.ListViewItem

            lvi = New System.Windows.Forms.ListViewItem(New String() {wl.dog.dogName}, Me.lvKennel.Items.Count)
            Me.lvKennel.Items.Add(lvi)
            lvi = Nothing
        Else

        End If

    End Sub

    Private Sub DELETEToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DELETEToolStripMenuItem.Click
        If lvKennel.SelectedItems.Count > 0 Then

            Dim dog As WatchDog = Me.watchDogs(lvKennel.SelectedItems.Item(0).Index)
            dog.Stop_Watch()
            Me.watchDogs.RemoveAt(lvKennel.SelectedItems.Item(0).Index)
            Me.lvKennel.Items.RemoveAt(lvKennel.SelectedItems.Item(0).Index)

            Dim bones() As String
            bones = MainModule.Read(dog.dogName, "BONES").Split(",")

            For Each item As String In bones
                MainModule.Delete(dog.dogName & "@\@" & item)
            Next

            MainModule.Delete(dog.dogName)

            Dim strMonitors() As String
            strMonitors = MainModule.Read("KENNEL_SECTION", "KENNEL_KEY").Split(",")

            Dim strResult As String = ""
            For Each item As String In strMonitors
                If Not item.Equals(dog.dogName) Then
                    strResult = strResult & item & ","
                End If
            Next

            If strResult.IndexOf(",") > 0 Then
                strResult = strResult.Substring(0, strResult.Length - 1)
            End If
            MainModule.Write("KENNEL_SECTION", "KENNEL_KEY", strResult)

        End If

    End Sub

    Private Sub btnHide_Click(sender As Object, e As EventArgs) Handles btnHide.Click
        Me.Close()
    End Sub
End Class